//
//  BSTableViewController.m
//  tableview多选
//
//  Created by 毕帅 on 2016/11/2.
//  Copyright © 2016年 毕帅. All rights reserved.
//

#import "BSTableViewController.h"
#import "BSTableViewCell.h"

@interface BSTableViewController ()

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableSet *selectedSet;

@end

@implementation BSTableViewController

static NSString *const reuseIdentifier = @"cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
}

- (void)setupView {
    self.navigationItem.title = @"tableView多选";
    [self.tableView registerNib:[UINib nibWithNibName:@"BSTableViewCell" bundle:nil] forCellReuseIdentifier:reuseIdentifier];
    self.tableView.tableFooterView = [[UIView alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLable.text = self.dataArray[indexPath.row];
    cell.isSelectedCell = NO;
    if ([self.selectedSet containsObject:indexPath]) {
        cell.isSelectedCell = YES;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.selectedSet containsObject:indexPath]) {
        [self.selectedSet removeObject:indexPath];
    } else {
        [self.selectedSet addObject:indexPath];
    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - lazyload
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        for (NSInteger i; i < 50; i++) {
            [_dataArray addObject:[NSString stringWithFormat:@"第%lu行", i]];
        }
    }
    return _dataArray;
}

- (NSMutableSet *)selectedSet {
    if (!_selectedSet) {
        _selectedSet = [NSMutableSet set];
    }
    return _selectedSet;
}



@end
